package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author 爱坤的代码
 * @since 2024-08-20
 */
@Api(tags="我得课程表相关接口")
@RestController
@RequestMapping("/lessons")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService lessonService;
    @ApiOperation("查询我的课表，排序字段 latest_learn_time:学习时间排序，create_time:购买时间排序")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> queryMyLessons(PageQuery Query) {
        return lessonService.queryMyLessons(Query);
    }

    @ApiOperation("查询当前正在学习的课程")
    @GetMapping("/now")
    public LearningLessonVO queryMyLessonNow() {
        return lessonService.queryMyLessonNow();
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("/{courseId}")
    public LearningLessonVO queryMyLessonByCourseId(@ApiParam(value = "课程id" ,example = "1")
                                                        @PathVariable("courseId") Long courseId) {
        return lessonService.queryMyLessonByCourseId(courseId);
    }

    @ApiOperation("根据id删除课程")
    @DeleteMapping("/{courseId}")
    public void deleteMyLessonByCourseId(@ApiParam(value = "课程id" ,example = "1")
                                             @PathVariable("courseId") Long courseId) {
        lessonService.deleteMyLessonByCourseId(null,courseId);
    }

    @ApiOperation("查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO queryMyPlans(PageQuery query){
        return lessonService.queryMyPlans(query);
    }

    @ApiOperation("创建我的学习计划")
    @PostMapping("/plans")
    public void createLearningPlan(@Valid @RequestBody LearningPlanDTO planDTO){
        lessonService.createLearningPlan(planDTO.getCourseId(), planDTO.getFreq());

    }

    @ApiOperation("校验当前的课程是否已经报名")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId){
        return lessonService.isLessonValid(courseId);
    }

    @ApiOperation("统计课程学习人数")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(
            @ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId){
        return lessonService.countLearningLessonByCourse(courseId);
    }
}
